package com.pzb.page.service.impl;


import com.pzb.goods.domain.GdGoods;
import com.pzb.goods.mapper.GdGoodsMapper;
import com.pzb.page.domain.*;
import com.pzb.page.mapper.*;
import com.pzb.page.service.IPgPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 页面管理Service业务层处理
 * 
 * @author pzb
 * @date 2021-09-17
 */
@Service
public class PgPageServiceImpl implements IPgPageService
{
    @Autowired
    private PgPageMapper pgPageMapper;
    @Autowired
    private PgPageItemMapper pgPageItemMapper;
    @Autowired
    private PgPageItemScrollposterMapper scrollposterMapper;
    @Autowired
    private PgPageItemEjectMapper ejectMapper;
    @Autowired
    private PgPageItemNoticeMapper noticeMapper;
    @Autowired
    private PgPageItemHotspotareaMapper hotspotareaMapper;
    @Autowired
    private PgPageItemGoodsMapper itemGoodsMapper;
    @Autowired
    private GdGoodsMapper gdGoodsMapper;
    @Autowired
    private PgPageItemVideoMapper videoMapper;
    @Autowired
    private PgPageItemGroupbuyingMapper groupbuyingMapper;
    @Autowired
    private PgPageItemPanicbuyingMapper panicbuyingMapper;
    @Autowired
    private PgPageItemPictureMapper pictureMapper;
    @Autowired
    private PgSearchMapper searchMapper;

    /**
     * 查询页面管理
     *
     * @param extAppid 页面管理主键
     * @return 页面管理
     */
    @Override
    public PgPage selectHomePage(String extAppid){
        PgPage page = pgPageMapper.selectHomePage(extAppid);
        if (page!=null){
            List<PgPageItem> itemList = getPageItemList(page.getId());
            page.setItemList(itemList);
        }
        return page;
    }

    @Override
    public PgPage getPageInfo(Long pageId) {
        PgPage page = pgPageMapper.selectPageById(pageId);
        if (page!=null){
            List<PgPageItem> itemList = getPageItemList(page.getId());
            page.setItemList(itemList);
        }
        return page;
    }

    @Override
    public List<PgSearch> selectPgSearchList(String extAppid) {
        return searchMapper.selectPgSearchList(extAppid);
    }

    //
    public List<PgPageItem> getPageItemList(Long pageId) {
        List<PgPageItem> itemList = pgPageItemMapper.selectPgPageItemList(pageId);
        for (PgPageItem item : itemList) {
            if (item.getItemCode().intValue() == 1) {
                List<PgPageItemScrollposter> scrollposterList = scrollposterMapper.selectScrollposterListByItemId(item.getId());
                item.setScrollposterList(scrollposterList);
            }else if (item.getItemCode().intValue() == 2) {
                PgPageItemEject eject = ejectMapper.selectEjectByItemId(item.getId());
                item.setEject(eject);
            }else if (item.getItemCode().intValue() == 3) {
                PgPageItemNotice notice =noticeMapper.selectNoticeByItemId(item.getId());
                item.setNotice(notice);
            }else if (item.getItemCode().intValue() == 4) {
                List<PgPageItemHotspotarea> hotspotareaList = hotspotareaMapper.selectHotspotareaListByItemId(item.getId());
                item.setHotspotareaList(hotspotareaList);
            }else if (item.getItemCode().intValue() == 5) {
                PgPageItemGoods itemGoods = itemGoodsMapper.selectItemGoodsByItemId(item.getId());
                //查询商品列表
                if (itemGoods!=null){
                    List<GdGoods> goodsList = gdGoodsMapper.selectItemGoodsListByItemId(item.getId());
                    itemGoods.setItemGoodsList(goodsList);
                    item.setItemGoods(itemGoods);
                }
            }else if (item.getItemCode().intValue() == 6) {
                PgPageItemVideo video = videoMapper.selectVideoByItemId(item.getId());
                item.setVideo(video);
            }else if (item.getItemCode().intValue() == 7) {
                PgPageItemGroupbuying groupbuying = groupbuyingMapper.selectGroupbuyingByItemId(item.getId());
                //查询展示商品
                if(groupbuying!=null){
                    List<GdGoods> goodsList=gdGoodsMapper.selectGroupbuyGoodsListByGroupId(Long.parseLong(groupbuying.getLinkAddress()));
                    groupbuying.setGroupbuyGoodsList(goodsList);
                    item.setGroupbuy(groupbuying);
                }
            }else if (item.getItemCode().intValue() == 8) {
                PgPageItemPanicbuying panicbuying = panicbuyingMapper.selectPanicbuyingByItemId(item.getId());
                //查询展示商品
                if(panicbuying!=null){
                    List<GdGoods> goodsList=gdGoodsMapper.selectPanicbuyGoodsListByPanicId(Long.parseLong(panicbuying.getLinkAddress()));
                    panicbuying.setPanicbuyGoodsList(goodsList);
                    item.setPanicbuy(panicbuying);
                }

            }else if (item.getItemCode().intValue() == 9) {
                PgPageItemPicture picture = pictureMapper.selectPictureByItemId(item.getId());
                item.setPicture(picture);
            }
        }
        return itemList;
    }

}
